/**
 * 
 * @Author: biubiu0002
 * @Date: 2021-04-26 11:43
 **/

#include <Class.h>
#include <iostream>
#include "ClassMapper.h"
#include "ClassService.h"
#include "vector"
#include "list"

using namespace std;




void ClassService::showAllClassList(){
    ClassMapper classMapper;

    this->showClassList(classMapper.getClassList());

}

void ClassService::showClassList(list<Class> classList){
    for(Class aClass:classList){
        cout<<aClass.getClassId()<<" ";
        cout<<aClass.getGrade()<<" ";
        cout<<aClass.getCollege()<<" ";
        cout<<aClass.getMajor()<<" ";
        cout<<aClass.getClassNo()<<endl;
    }

}

void ClassService::showClassList(vector<Class> classList){
    for(Class aClass:classList){
        cout<<aClass.getClassId()<<" ";
        cout<<aClass.getGrade()<<" ";
        cout<<aClass.getCollege()<<" ";
        cout<<aClass.getMajor()<<" ";
        cout<<aClass.getClassNo()<<endl;
    }

}
int ClassService::batchInsertClass() {
    ClassMapper classMapper;
    list<Class> classList = classMapper.getClassList();
    int onceCommitChoice = -1;
    vector<Class> tempClassList;
    while(true){
        string field;
        cout<<"请输入班级编号(10位统一编号):";
        cin>>field;
        int idIsRepeated = this->checkId(field,tempClassList);
        if(idIsRepeated){
            cout<<"课程编号有重复！请重试"<<endl;
            if(isContinue(classList,tempClassList)){
                continue;
            }
            return 0;

        }
        Class* aClass = new Class();
        aClass->setClassId(field);
        cout<<"请输入班级所在年级：";cin>>field;
        aClass->setGrade(stoi(field));
        cout<<"请输入班级所在学院：";cin>>field;
        aClass->setCollege(field);
        cout<<"请输入班级的专业：";cin>>field;
        aClass->setMajor(field);
        cout<<"请输入班级的班级号（如1，2，3班，只输入数字）：";cin>>field;
        aClass->setClassNo(stoi(field));

        cout<<endl<<"您输入的课程信息是："<<endl;
        cout<<"班级编号\t年级\t学院\t专业\t班级号"<<endl;
        cout<<aClass->getClassId()<<" ";
        cout<<aClass->getGrade()<<" ";
        cout<<aClass->getCollege()<<" ";
        cout<<aClass->getMajor()<<" ";
        cout<<aClass->getClassNo()<<endl;
        cout<<endl<<"确认该条记录正确吗?(1.正确，加入到暂存区  2.错误，重新输入  0.放弃输入，退回上一页面)";
        cin>>onceCommitChoice;
        if(onceCommitChoice==0){
            cout<<"退出成功";
            system("pause");
            system("cls");
            return 0;
        }
        else if(onceCommitChoice==2){
            continue;
        }
        tempClassList.push_back(*aClass);
        cout<<"当前已暂存的信息："<<endl;
        cout<<"班级编号\t年级\t学院\t专业\t班级号"<<endl;
        for(Class tempClass:tempClassList){
            cout<<tempClass.getClassId()<<" "<<tempClass.getGrade()<<" ";
            cout<<tempClass.getCollege()<<" "<<tempClass.getMajor()<<" ";
            cout<<tempClass.getClassNo()<<endl;
        }
        if(isContinue(classList,tempClassList)){
            continue;
        }
        delete(aClass);
        return 0;


    }

}

int ClassService::isContinue(list<Class> &classList, vector<Class> &tempClassList) {
    int continueChoice,saveResultChoice;
    cout<<endl<<"是否继续录入？(1.继续录入 0.结束录入)";
    cin>>continueChoice;
    if(continueChoice==1){
        return 1;
//       returns to continue;
    }
    else if(continueChoice==0){
        cout<<"是否保存结果信息？(1.保存 0.放弃，本次录入将不被保存)"<<endl;
        cin>>saveResultChoice;
        if(saveResultChoice){
            for(Class aClass:tempClassList){
                classList.push_back(aClass);
            }
            ClassMapper classMapper;
            classMapper.setClassList(classList);
            classMapper.batchInsertClassList(classList);
            cout<<"保存成功！"<<endl;
        }
        else{
            cout<<"退出成功。"<<endl;
        }
        system("cls");
        return 0;
    }
    cout<<"出错啦！回到上一页面"<<endl;
    return 0;
}

int ClassService::checkId(std::string id,vector<Class>& tempClassList){
    ClassMapper classMapper;
    list<Class> classList =  classMapper.getClassList();
    for(Class aClass:classList){
        if(aClass.getClassId()==id){
            return 1;
        }
    }
    for(Class aClass:tempClassList){
        if(aClass.getClassId()==id){
            return 1;
        }
    }
    return 0;

}

ClassService::ClassService() {

}

ClassService::~ClassService() {

}